﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;
using Newtonsoft.Json;

namespace Bot.Domain.Categories.Service
{
    public class AnswerInChannelPromptDomainService : IAnswerInChannelPromptDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, AnswerInChannelPrompt> _repository;

        public AnswerInChannelPromptDomainService(IRepository<Guid, AnswerInChannelPrompt> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public AnswerInChannelPrompt Create(AnswerInChannelPrompt category)
        {
            return _repository.Create(category);
        }

        public AnswerInChannelPrompt Get(Guid id, string include = "")
        {
            var promt = _repository.Get(id);
            if(promt!=null
                && promt.EntityId != Guid.Empty
                && !string.IsNullOrEmpty("include")
                && include.Split(',').Contains("entity"))
            {
                IEntityDomainService entityDomainService = _container.Resolve<IEntityDomainService>();
                promt.Entity = entityDomainService.Get(promt.EntityId, include);
            }
            return promt;
        }

        public AnswerInChannelPrompt Update(AnswerInChannelPromptUpdateBo bo)
        {
            AnswerInChannelPrompt answerInChannelPrompt = _repository.Get(bo.Id);
            answerInChannelPrompt.Id = bo.Id;
            answerInChannelPrompt.AnswerInChannelId = bo.AnswerInChannelId;
            answerInChannelPrompt.EntityId = bo.EntityId;
            answerInChannelPrompt.EntityLabel = bo.EntityLabel;
            answerInChannelPrompt.Question = bo.Question;
            answerInChannelPrompt.Options = JsonConvert.SerializeObject(bo.Options);
            answerInChannelPrompt.Order = bo.Order;

            return answerInChannelPrompt;

        }

        public IEnumerable<AnswerInChannelPrompt> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            if (_repository.Exists(id))
            {
                AnswerInChannelPrompt answerInChannelPrompt = _repository.Get(id);

                _repository.Delete(answerInChannelPrompt);
            }
        }

        public IEnumerable<AnswerInChannelPrompt> GetByAnswerInChannelId(Guid answerInChannelId, string include = "")
        {
            var list = _repository.List(new AnswerInChannelPromptFilterSpecification(answerInChannelId)).ToList();
            if(list!=null&& list.Count>0&&!string.IsNullOrEmpty(include)&& include.Split(',').Contains("entity"))
            {
                IEntityDomainService entityDomainService = _container.Resolve<IEntityDomainService>();
                foreach (var item in list)
                {
                    if (item.EntityId != Guid.Empty)
                    {
                        item.Entity = entityDomainService.Get(item.EntityId,include);
                    }
                }
            }
            return list; 
        }
        public AnswerInChannelPrompt CreateWithAnswerInChannelId(Guid answerInChannelId, AnswerInChannelPrompt answerInChannelPrompt)
        {
            answerInChannelPrompt.AnswerInChannelId = answerInChannelId;
            answerInChannelPrompt = Create(answerInChannelPrompt);
            return answerInChannelPrompt;
        }
    }
}
